//对返回结果进行数据转化，包括添加图片前缀，判断商品能否购买多个
(function($){
	$.extend($, {
		DATA_CONVERTER : (function(){
			var selfDefinePrefix = $.CONSTANT.IMG_PREFIX.SELF_DEFINE;
			var defaultImgPrefix = $.CONSTANT.IMG_PREFIX.DEFAULT;
			var self = {};
			
			function getPrefix(serverPrefix){
				return selfDefinePrefix || serverPrefix || defaultImgPrefix;
			};
			var converters = {};
			converters[ $.CONSTANT.API.INDEX_AREA ] = function(data){//首页专区商品
				var prefix = getPrefix(data.bean['ftpUrl']);
				if(prefix){
					$(data.beans).each(function(i, unitItem){
						unitItem.MCDS_PIC_URL = prefix + unitItem.MCDS_PIC_URL;
					});
				}
			};
			converters[ $.CONSTANT.API.SEARCH ] = function(data){//搜索
				var prefix = getPrefix(data.bean['ftpUrl']);
				if(prefix){
					$(data.beans).each(function(i, unitItem){
						unitItem.picStoUrlAddr = prefix + unitItem.picStoUrlAddr;
					});
				}
			}
			converters[ $.CONSTANT.API.LIMIT_AREA ] = function(data){//首页专区商品
				var prefix = getPrefix(data.bean['ftpUrl']);
				if(prefix){
					$(data.beans).each(function(i, unitItem){
						unitItem.mcdsPic = prefix + unitItem.mcdsPic;
					});
				}
			};
			converters[ $.CONSTANT.API.RANK ] = function(data){//排行榜
				var prefix = getPrefix(data.bean['ftpUrl']);
				if(prefix){
					$(data.beans).each(function(i, unitItem){
						unitItem.mcdsPicture = prefix + unitItem.mcdsPicture;
					});
				}
			};
			converters[ $.CONSTANT.API.UNIT_LIST ] = function(data){//商品详情
				var prefix = getPrefix(data.bean['ftpUrl']);
				if(prefix){
					$(data.object).each(function(i, unitItem){
						
						//判断商品能否购买多个
						unitItem.multiple = $.CONSTANT.PTY.checkSupportedPty(unitItem.PTY_ID);
						
						$(unitItem.image).each(function(j, images){
							for(var imageType in images){
								images[imageType] = prefix + images[imageType];
							}
						});
					});
				}
			};
			converters[ $.CONSTANT.API.UNIT_DETAIL ] = function(data){//商品图文详情
				var prefix = getPrefix(data.bean['ftpUrl']);
				if(prefix){
					$(data.beans).each(function(i, imageDetail){
						imageDetail.PIC_SRC = prefix + imageDetail.PIC_SRC;
					})
				}
			};
			converters[ $.CONSTANT.API.CART_LIST ] = function(data){//购物车列表
				var prefix = getPrefix(data.bean['ftpUrl']);
				if(prefix){
					$(data.beans).each(function(i, unitItem){
						
						//判断商品能否购买多个
						unitItem.multiple = $.CONSTANT.PTY.checkSupportedPty(unitItem.suplerId);
						
						unitItem.mcdsPhoto = prefix + unitItem.mcdsPhoto;
					})
				}
			};
			converters[ $.CONSTANT.API.SUB_ORDER_LIST ] = function(data){//子订单列表
				var prefix = getPrefix(data.bean['ftpUrl']);
				if(prefix && data.object.list){
					$(data.object.list).each(function(i, order){
						if(order.mcdsUnitList){
							$(order.mcdsUnitList).each(function(j, item){
								item.mainPicSrc = prefix + item.mainPicSrc;
							});
						}
					});
				}
				console.log(data);
			};
			converters[ $.CONSTANT.API.MAIN_ORDER_DETAIL ] = function(data){//主订单详情
				var prefix = getPrefix(data.bean['ftpUrl']);
				if(prefix && data.object && data.object.subOdrList){
					$(data.object.subOdrList).each(function(i, order){
						$(order.sbscrMcdsList).each(function(j, item){
							item.mainPicSrc = prefix + item.mainPicSrc;
						});
					})
				}
			};
			converters[ $.CONSTANT.API.SUB_ORDER_DETAIL ] = function(data){//子订单详情
				var prefix = getPrefix(data.bean['ftpUrl']);
				if(prefix){
					if(data.object && data.object.sbscrMcdsList){
						$(data.object.sbscrMcdsList).each(function(i, unitItem){
							unitItem.mainPicSrc = prefix + unitItem.mainPicSrc;
						});
					}
				}
			};
			
			self.get = function(name){
				return converters[name];
			};
			
			self.convert = function(name, data){
				var converter = converters[name];
				try{
					converter && converter(data);
				}catch(e){
					console.log(e);
				}
			};
			
			return self;
		})()
	});
})(jQuery);
